package com.mxnet.chongjin.supoman.activity;

import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mxnet.chongjin.supoman.MainActivity;
import com.mxnet.chongjin.supoman.R;
import com.mxnet.chongjin.supoman.config.Api;
import com.mxnet.chongjin.supoman.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;

public class UserLoginActivity extends AppCompatActivity implements View.OnClickListener {
    private Context mContext;
    private LinearLayout mLinearLayout;
    private TextView mTextView, mTextView1, mTextView2;
    private EditText mEditText1, mEditText2;
    private Button mButton;
    private SharedPreferences mPreferences;
    private SharedPreferences.Editor mEditor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_login);
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        mContext = UserLoginActivity.this;
        mPreferences = getSharedPreferences("supoman", MODE_PRIVATE);
        mEditor = mPreferences.edit();

        initView();
        initData();
    }

    public void initData() {

    }

    public void initView() {
        mLinearLayout = findViewById(R.id.ll_user_login);
        mLinearLayout.setOnClickListener(this);

        mTextView = findViewById(R.id.tv_regist_user_login);
        mTextView.setOnClickListener(this);

        mTextView1 = findViewById(R.id.tv_regist_forget_pwd);
        mTextView1.setOnClickListener(this);

        mTextView2 = findViewById(R.id.tv_regist_change_repair);
        mTextView2.setOnClickListener(this);

        mEditText1 = findViewById(R.id.et_user_login_account);

        mEditText2 = findViewById(R.id.et_user_login_pwd);

        mButton = findViewById(R.id.btn_user_login);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        final Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.ll_user_login:
                new Thread() {
                    public void run() {
                        try {
                            Instrumentation inst = new Instrumentation();
                            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
            case R.id.btn_user_login:
                if (!TextUtils.isEmpty(mEditText1.getText()) && !TextUtils.isEmpty(mEditText2.getText())) {
                    OkHttpUtils.get()
                            .url(Api.URL_HEADER + Api.CUSTOMER_LOGIN)
                            .addParams("userCode", mEditText1.getText().toString())
                            .addParams("password", mEditText2.getText().toString())
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    ToastUtils.showToast(mContext, "登录失败！");
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        boolean isSuccess = jsonObject.optBoolean("success");
                                        if (isSuccess) {
                                            JSONObject data = jsonObject.optJSONObject("data");
                                            mEditor.putBoolean("isLogin", true);
                                            mEditor.putString("userId", data.optString("id"));
                                            mEditor.putString("userImgUrl", data.optString("userImgUrl"));
                                            mEditor.putString("userName", data.optString("userName"));
                                            mEditor.putString("userCode", data.optString("userCode"));
                                            mEditor.commit();
                                            ToastUtils.showToast(mContext, getResources().getString(R.string.login_success));
                                            intent.setClass(mContext, MainActivity.class);
                                            startActivityForResult(intent, 0);
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                } else {
                    ToastUtils.showToast(mContext, "请输入账号和密码！");
                }
                break;
            case R.id.tv_regist_user_login:
                intent.setClass(mContext, RegistActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.tv_regist_forget_pwd:
                intent.setClass(mContext, ForgetPwdActivity.class);
                mContext.startActivity(intent);
                break;
            case R.id.tv_regist_change_repair:
                intent.setClass(mContext, MaintainerLoginActivity.class);
                mContext.startActivity(intent);
                break;
        }
    }
}
